package com.zdtech.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zdtech.dao.TBillDAO;
import com.zdtech.dao.TKindDAO;
import com.zdtech.dao.TMenuDAO;
import com.zdtech.dao.TNavDAO;
import com.zdtech.dao.TSingleDAO;
import com.zdtech.dao.TUserDAO;
import com.zdtech.dao.VPurviewnavDAO;
import com.zdtech.entity.TBill;
import com.zdtech.entity.TKind;
import com.zdtech.entity.TMenu;
import com.zdtech.entity.TSingle;
import com.zdtech.entity.TUser;
import com.zdtech.entity.VPurviewnav;
import com.zdtech.service.BaseSerive;

@Service("BaseSerive")
public class BaseServiceImpl implements BaseSerive {
	private String hql;
	@Resource
	private TMenuDAO tMenuDAO;
	@Resource
	private TKindDAO tKindDAO;
	@Resource
	private TSingleDAO tSingleDAO;
	@Resource
	private TBillDAO tBillDAO;
	@Resource
	private TUserDAO tUserDAO;
	@Resource
	private TNavDAO tNavDAO;
	@Resource
	private VPurviewnavDAO vPurviewnavDAO;
	
	public Integer insertKind(TKind o) {
		return (Integer) tKindDAO.save(o);
	}

	public Integer insertMenu(TMenu o) {
		return (Integer) tMenuDAO.save(o);
	}

	public List<TMenu> showMenuAll() {
		hql = "from TMenu";
		return tMenuDAO.find(hql);
	}

	public TMenu getMenu(int id) {
		return tMenuDAO.get(TMenu.class, id);
	}

	public void editMenu(TMenu m) {
		tMenuDAO.update(m);
	}

	public void delMenu(int id) {
		tMenuDAO.delete(getMenu(id));
	}

	public boolean insertSingle(TSingle s) {
		Integer id = (Integer) tSingleDAO.save(s);
		if(id.SIZE>0){
			return true;
		}
		return false;
	}

	public Integer insertBill(TBill b) {
		return (Integer) tBillDAO.save(b);
	}

	public boolean insertUser(TUser u) {
		Integer id = (Integer) tUserDAO.save(u);
		if(id.SIZE>0){
			return true;
		}
		return false;
	}

	public List<TUser> showUserAll() {
		hql = "from TUser";
		return tUserDAO.find(hql);
	}

	public void editUser(TUser u) {
		tUserDAO.update(u);
	}

	public void delUser(int id) {
		tUserDAO.delete(getUser(id));
	}

	public TUser getUser(int id) {
		return tUserDAO.get(TUser.class, id);
	}

	public boolean checkUser(String username, String password) {
		hql = "from TUser u where u.user = '"+username+"'";
		List<TUser> userlist = tUserDAO.find(hql);
		if(userlist.size()>0){
			TUser user = userlist.get(0);
			if(user.getPwd().equals(password)){
				return true;
			}
		}
		return false;
	}
	
	public List<VPurviewnav> showNav(String pur) {
		hql = "from VPurviewnav p where p.id.id ="+1;
		return vPurviewnavDAO.find(hql);
	}

}
